package org.kinglone.back.article.service;

import org.hibernate.Query;
import org.kinglone.back.article.model.Article;
import org.kinglone.back.comment.model.Comment;
import org.kinglone.common.dao.impl.BaseDaoImpl;
import org.kinglone.common.pager.PageModel;
import org.kinglone.common.util.PageQuery;
import org.kinglone.common.util.PageResults;
import org.kinglone.common.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("articleService")
@Transactional(readOnly=false)
public class ArticleServiceImpl extends BaseDaoImpl<Article> implements ArticleService {


    @Override
    public PageResults<Article> findArticleByPage(PageQuery pageQuery, String title){
        StringBuffer hql = new StringBuffer("from Article a where 1=1 ");
        StringBuffer whereHql = new StringBuffer();
        Map<String, Object> queryParamMap = new HashMap<String, Object>();
       
    	if (!StringUtil.isNullOrEmpty(title)) {
			whereHql.append(" and title like :title ");
			queryParamMap.put("title", '%'+ title +'%');
		}
    	System.out.println("标题=======："+title);
    	whereHql.append(" order by publishDatetime desc");
        StringBuffer countsql = new StringBuffer(" select count(0) from article where 1=1 ");
        PageResults<Article> pageResults = super.findPageByHqlAndCountSql(hql.append(whereHql).toString(),countsql.append(whereHql).toString(),pageQuery, queryParamMap, queryParamMap);
        return pageResults;
    }

	@Override
	public List<Article> findByPage(PageModel pageModel,String classifyId,String title) {
		StringBuffer hql = new StringBuffer();	
		List<String> param =  new ArrayList<>();
		hql.append("from Article a where 1=1 ");
		if(!StringUtil.isNullOrEmpty(classifyId)) {
			hql.append(" and a.classifyId =?");
			param.add(0, classifyId);
		}
		if(!StringUtil.isNullOrEmpty(title)){
			hql.append(" and a.title like ?");
			param.add(0,'%'+title+'%');
		}
		
		hql.append(" and a.isDisplay ='Y' order by a.publishDatetime desc");
		return this.findByPage(hql.toString(), pageModel, param);
	}

	@Override
	public List<Article> getByClassifyId(String classifyId) {
		String hql = "from Article a where a.classifyId =?";
		Query query = this.getSession().createQuery(hql);
		query.setParameter(0, classifyId);
		List<Article> list = query.list();
		return list;
	}

	@Override
	public List<Article> getArticleByNum() {
		String sql = "select a.sysid,a.title from article a where a.isDisplay ='Y' order by a.modifyTime  desc limit 8";
		Query query = this.getSession().createSQLQuery(sql);
		List<Object[]> list = query.list();
		List<Article> articles= new ArrayList<Article>();
		for (Object[] objects : list) {
			Article a = new Article();
			a.setSysid(objects[0].toString());
			a.setTitle(objects[1].toString());
			articles.add(a);
		}
		return articles;
	}

	@Override
	public BigInteger getArticleCount(String classifyId) {
		String sql = "select IFNULL(count(*),0) from article where isDisplay ='Y' and classifyId =?";
		Query query = this.getSession().createSQLQuery(sql);
		query.setParameter(0, classifyId);
		return (BigInteger)query.list().get(0);
	}

	@Override
	public Article findUpperArticleById(String publishDatetime) {
		String sql = "SELECT a.sysid,a.title from article a where a.isDisplay ='Y' and a.publishDatetime > :publishTime  ORDER BY a.publishDatetime ASC limit 1";
		Query query = this.getSession().createSQLQuery(sql);
		query.setParameter("publishTime", publishDatetime);
		List<Object[]> list = query.list();
		List<Article> articles= new ArrayList<Article>();
		for (Object[] objects : list) {
			Article a = new Article();
			a.setSysid(objects[0].toString());
			a.setTitle(objects[1].toString());
			articles.add(a);
		}
		 if(articles!=null&&articles.size()!=0){
	            return articles.get(0);
	        }else{
	        	return null;
	        }

	}

	@Override
	public Article findNextArticleById(String publishDatetime) {
		String sql = "SELECT a.sysid,a.title from article a where  a.isDisplay ='Y' and  a.publishDatetime < :publishTime ORDER BY a.publishDatetime DESC limit 1;";
		Query query = this.getSession().createSQLQuery(sql);
		query.setParameter("publishTime", publishDatetime);
		List<Object[]> list = query.list();
		List<Article> articles= new ArrayList<Article>();
		for (Object[] objects : list) {
			Article a = new Article();
			a.setSysid(objects[0].toString());
			a.setTitle(objects[1].toString());
			articles.add(a);
		}
		 if(articles!=null&&articles.size()!=0){
	            return articles.get(0);
	        }else{
	        	return null;
	        }
	}



}
